package com.ddstation.crm.menu.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crm.login.util.WnSessionUtil;
import com.ddstation.crm.menu.entity.DtInvsManageInstitutionMenuEntity;
import com.ddstation.crm.menu.entity.DtInvsManageInstitutionMenuPanelEntity;
import com.ddstation.crm.menu.service.MENU_FILTER_ENUM;
import com.ddstation.crm.menu.service.MenuService;

/**
 * 《卫监》 菜单 panel模式
 * @author zhangdifeng
 * @date 2018年5月30日
 */
@Controller
public class PanelMenuController {

	@RequestMapping(value="/panel_main.do")
	public String initPanelMenuEvent(
			HttpSession session,
			Model model) throws IllegalAccessException {
		
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		
		return "menu/main_menu_panel";
	}
	
	/*@RequestMapping(value="wncrm_panel_main.do/load", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadPanelMenuEvent(HttpSession session) {
		// 从session获取登录用户基础数据
		String institutionId = WnSessionUtil.getInstitutionId(session);
		Integer supervisorId = WnSessionUtil.getSupervisorId(session);
		
		Map<String, Object> retMap = new HashMap<String, Object>();
//		retMap.put("hcrmaccess", WnSessionUtil.hcrmAccess(session)); // HCRM使用状况
		
		final int LEFT_MENU = 1, RIGHT_MENU = 2;
		List<DtInvsManageInstitutionMenuPanelEntity> leftMenus = new ArrayList<DtInvsManageInstitutionMenuPanelEntity>();
		List<DtInvsManageInstitutionMenuPanelEntity> rightMenus = new ArrayList<DtInvsManageInstitutionMenuPanelEntity>();
		
		// panel菜单 设置
		List<DtInvsManageInstitutionMenuPanelEntity> panelMenus = menuService.getPanelMenu();
		if (!panelMenus.isEmpty()) {
			// 纵向菜单 匹配
			List<DtInvsManageInstitutionMenuEntity> menus = menuService.getVerticalMenu(institutionId, supervisorId, MENU_FILTER_ENUM.INSTITUTION_AND_DOCTOR);
			if (!menus.isEmpty()) {
				Iterator<DtInvsManageInstitutionMenuPanelEntity> panelMenusIte = panelMenus.iterator();
				while (panelMenusIte.hasNext()) {
					DtInvsManageInstitutionMenuPanelEntity p = panelMenusIte.next();
					if (p.getMenuType() == 1) {
						DtInvsManageInstitutionMenuEntity m = this.findEqual(p, menus);
						if (m != null) {
							p.setName(m.getName());
							p.setUrl(m.getUrl());
							p.setIcon1(m.getIcon1());
							p.setIcon2(m.getIcon2());
							p.setAvailable(m.isAvailable());
							if (p.getPositionType() == LEFT_MENU) {
								leftMenus.add(p);
							} else if (p.getPositionType() == RIGHT_MENU) {
								rightMenus.add(p);
							}
							panelMenusIte.remove();
						}
					}
				}
			}
			
			// 横向菜单 匹配
			List<DtInvsManageInstitutionMenuEntity> horizonatalMenus = menuService.getHorizontalMenu(institutionId, supervisorId, MENU_FILTER_ENUM.INSTITUTION_AND_DOCTOR);
			if (!horizonatalMenus.isEmpty()) {
				retMap.put("horizonatalMenus", horizonatalMenus); // 服务菜单
				
				Iterator<DtInvsManageInstitutionMenuPanelEntity> panelMenusIte = panelMenus.iterator();
				while (panelMenusIte.hasNext()) {
					DtInvsManageInstitutionMenuPanelEntity p = panelMenusIte.next();
					if (p.getMenuType() == 2) {
						DtInvsManageInstitutionMenuEntity m = this.findEqual(p, horizonatalMenus);
						if (m != null) {
							p.setName(m.getName());
							p.setUrl(m.getUrl());
							p.setIcon1(m.getIcon1());
							p.setIcon2(m.getIcon2());
							p.setWorkFlowType(m.getWorkFlowType());
							p.setAvailable(m.isAvailable());
							if (p.getPositionType() == LEFT_MENU) {
								leftMenus.add(p);
							} else if (p.getPositionType() == RIGHT_MENU) {
								rightMenus.add(p);
							}
							panelMenusIte.remove();
						}
					}
				}
			}
		}
		
		// 菜单排序
		MyTaskComparator menuSort = new MyTaskComparator();
		Collections.sort(leftMenus, menuSort);
		Collections.sort(rightMenus, menuSort);
		
		retMap.put("errcode", 0);
		retMap.put("leftMenus", leftMenus);
		retMap.put("rightMenus", rightMenus);
		
		return retMap;
	}*/
	
	/**
	 * 查找相同项目
	 * @param one
	 * @param menus
	 * @return
	 */
	private DtInvsManageInstitutionMenuEntity findEqual(DtInvsManageInstitutionMenuPanelEntity p, List<DtInvsManageInstitutionMenuEntity> menus) {
		for (DtInvsManageInstitutionMenuEntity m : menus) {
			if (p.getMenuId().intValue() == m.getId().intValue()) {
				return m;
			} else if (m.getChildrens() != null && !m.getChildrens().isEmpty()) {
				DtInvsManageInstitutionMenuEntity _m = findEqual(p, m.getChildrens());
				if (_m != null) {
					return _m;
				}
			}
		}
		return null;
	}
	
	
	@Inject private MenuService menuService = null;
//	@Inject private DdDoctorServiceItf doctorService = null;
}

class MyTaskComparator implements Comparator<DtInvsManageInstitutionMenuPanelEntity> {
	public final static int UP = -1;
	public final static int DOWN = 1;
	
	public int compare(DtInvsManageInstitutionMenuPanelEntity o1, DtInvsManageInstitutionMenuPanelEntity o2) {
		if(null != o1 && null != o2) {
			
			int t1 = o1.getOrderNb() == null ? 0 : o1.getOrderNb();
			int t2 = o2.getOrderNb() == null ? 0 : o2.getOrderNb();
			if(t1 >= t2)
				return MyTaskComparator.DOWN;
			else
				return MyTaskComparator.UP;
		} else {
			throw new RuntimeException("比较对象有null");
		}
	}
}